package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Set;

import models.ExercicioPadraoTreino;
import models.PadraoTreino;

public class DAOExercicioPadraoTreino {
	

	public Set<ExercicioPadraoTreino> select() throws Exception {
		Set<ExercicioPadraoTreino> objs = new HashSet<ExercicioPadraoTreino>();
		try {

			String query = "SELECT idPadraoTreino, idExercicio, repeticao, serie FROM ExercicioPadraoTreino";

			Statement statement = DAOConnection.getConnection().createStatement();

			ResultSet resultSet = statement.executeQuery(query);

			while(resultSet.next()) {
				ExercicioPadraoTreino obj = new ExercicioPadraoTreino();
				obj.idPadraoTreino = resultSet.getInt(1);
				obj.idExercicio = resultSet.getInt(2);
				obj.repeticao = resultSet.getInt(3);
				obj.serie = resultSet.getInt(4);
				objs.add(obj);
			}
		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
		return objs; 
	}

	public ExercicioPadraoTreino select(int idPadraoTreino, int idExercicio) throws Exception {
		ExercicioPadraoTreino obj = null;
		try	{

			String query = "SELECT idPadraoTreino, idExercicio, repeticao, serie FROM ExercicioPadraoTreino WHERE idPadraoTreino = ? AND idExercicio = ?";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(query);
			statement.setInt(1, idPadraoTreino);
			statement.setInt(2, idExercicio);
			
			ResultSet resultSet = statement.executeQuery();

			while(resultSet.next()) {
				obj = new ExercicioPadraoTreino();
				obj.idPadraoTreino = resultSet.getInt(1);
				obj.idExercicio = resultSet.getInt(2);
				obj.repeticao = resultSet.getInt(3);
				obj.serie = resultSet.getInt(4);
			}
		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
		return obj; 
	}

	public void insert(ExercicioPadraoTreino ept) throws Exception {
		try {
			String insert = "INSERT INTO ExercicioPadraoTreino (idPadraoTreino, idExercicio, repeticao, serie) VALUES(?, ?, ?, ?)";

			PreparedStatement ps = DAOConnection.getConnection().prepareStatement(insert);
			ps.setInt(1, ept.idPadraoTreino);
			ps.setInt(2, ept.idExercicio);
			ps.setInt(3, ept.repeticao);
			ps.setInt(4, ept.serie);

			ps.executeUpdate();
		} catch(SQLException e) {	
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
	}

	public void update(ExercicioPadraoTreino te) throws Exception {
		try {

			String update = "UPDATE ExercicioPadraoTreino SET repeticao = ?, serie = ? WHERE idPadraoTreino = ? AND idExercicio = ?";

			PreparedStatement ps = DAOConnection.getConnection().prepareStatement(update);
			ps.setInt(1, te.repeticao);
			ps.setInt(2, te.serie);
			ps.setInt(3, te.idPadraoTreino);
			ps.setInt(4, te.idExercicio);

			ps.executeUpdate();
		} catch(SQLException e) {
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}

	}

	public void delete(ExercicioPadraoTreino te) throws Exception {
		try {

			String delete = "DELETE FROM ExercicioPadraoTreino WHERE idPadraoTreino = ? AND idExercicio = ?";

			PreparedStatement ps = DAOConnection.getConnection().prepareStatement(delete);
			ps.setInt(1, te.idPadraoTreino);
			ps.setInt(2, te.idExercicio);

			ps.executeUpdate();
		} catch(SQLException e) {
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
	}
	
	public void delete(PadraoTreino pt) throws Exception {
		try {

			String delete = "DELETE FROM ExercicioPadraoTreino WHERE idPadraoTreino = ?";

			PreparedStatement ps = DAOConnection.getConnection().prepareStatement(delete);
			ps.setInt(1, pt.idPadraoTreino);

			ps.executeUpdate();
		} catch(SQLException e) {
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
	}

}